package com.edianping.store;

import java.util.*;
import java.sql.*;

import org.apache.log4j.Logger;

import com.edianping.bean.ProcessItemBean;
import com.edianping.bean.ReviewItemBean;
import com.edianping.bean.ShopItemBean;
import com.edianping.util.LogCode;

/**
 * @author jszhang
 * 
 */
public class ShopDBStore extends AbstractDBStore {

	protected static Logger log = Logger.getLogger(ShopDBStore.class);

	public static String[] GetShopInfoStatisticById(int shopId) {
		Connection conn = null;
		Statement stmt = null;
		String sql = "select tag,paymethod,feature from shop_info where id=" + shopId;
		
		String[] result = new String[3];
		try {
			conn = pool.getConnection();
			stmt = conn.createStatement();
			
			ResultSet rs = stmt.executeQuery(sql);
			while (rs.next()) {
				result[0] = rs.getString("tag")==null?"":rs.getString("tag");
				result[1] = rs.getString("paymethod")==null?"":rs.getString("paymethod");
				result[2] = rs.getString("feature")==null?"":rs.getString("feature");
			}
			
			rs.close();
			stmt.close();
		} catch (Exception e) {
			log.error("[[" + LogCode.E_DB_SQL_ERROR + "]], SQL:[" + sql + "]");
			log.debug(e);
			if (log.isDebugEnabled())
				log.debug(e);
		} finally {
			if (conn != null)
				pool.checkin(conn);
		}

		return result;
	}

	public static ProcessItemBean GetShopInfoById(int shopId) {
		Connection conn = null;
		Statement stmt = null;
		String sql = "select * from shop_info where id=" + shopId;
		ProcessItemBean b = null;

		try {
			conn = pool.getConnection();
			stmt = conn.createStatement();

			ResultSet rs = stmt.executeQuery(sql);

			ShopItemBean sbean;
			while (rs.next()) {
				String id = rs.getString("id");
				b = new ProcessItemBean(id);
				sbean = new ShopItemBean(id);
				sbean.setScore(rs.getInt("Score"));
				sbean.setShopid(rs.getString("id"));
				sbean.setReviews(ReviewDBStore.GetReviewsByShopId(Integer.parseInt(sbean
						.getShopid())));
				b.setBean(sbean);
			}

			rs.close();
			stmt.close();

		} catch (Exception e) {
			log.error("[[" + LogCode.E_DB_SQL_ERROR + "]], SQL:[" + sql + "]", e);
			if (log.isDebugEnabled())
				log.debug(e);
		} finally {
			if (conn != null)
				pool.checkin(conn);
		}

		return b;
	}

	public static List<ProcessItemBean> GetShopInfos(int startindex, int count) {
		List<ProcessItemBean> result = new ArrayList<ProcessItemBean>();
		Connection conn = null;
		Statement stmt = null;
		String sql = "select * from shop_info limit " + startindex + ", "
				+ count;

		try {
			conn = pool.getConnection();
			stmt = conn.createStatement();

			ResultSet rs = stmt.executeQuery(sql);

			ProcessItemBean b;
			ShopItemBean sbean;
			while (rs.next()) {
				String id = rs.getString("id");
				b = new ProcessItemBean(id);
				sbean = new ShopItemBean(id);
				sbean.setScore(rs.getInt("Score"));
				sbean.setShopid(rs.getString("id"));
				sbean.setReviews(ReviewDBStore.GetReviewsByShopId(Integer.parseInt(sbean
						.getShopid())));
				b.setBean(sbean);
				result.add(b);
			}

			rs.close();
			stmt.close();

		} catch (Exception e) {
			log.error("[[" + LogCode.E_DB_SQL_ERROR + "]], SQL:[" + sql + "]");
			log.debug(e);
			if (log.isDebugEnabled())
				log.debug(e);
		} finally {
			if (conn != null)
				pool.checkin(conn);
		}

		return result;
	}

	

	public static boolean UpdateShopInfo(ProcessItemBean item) {
		String sql = "update shop_info set " + "score="
				+ ((ShopItemBean) item.getBean()).getScore() + ", rate="
				+ ((ShopItemBean) item.getBean()).getRate() + ", service="
				+ ((ShopItemBean) item.getBean()).getService() + ", reality="
				+ ((ShopItemBean) item.getBean()).getReality() + ", velocity="
				+ ((ShopItemBean) item.getBean()).getVelocity() + " where id="
				+ item.getId();

		return ExecuteSQL(sql);
	}

	public static boolean UpdateTagPaymethodFeature(int id, String tag,
			String paymethod, String feature) {
		String sql = "update shop_info set " + "tag='" + tag + "'"
				+ ", paymethod='" + paymethod + "'" + ", feature='" + feature
				+ "'" + " where id=" + id;

		return ExecuteSQL(sql);
	}

	public static boolean UpdateShopInfos(List<ProcessItemBean> items) {
		List<String> sqls = new ArrayList<String>();

		for (ProcessItemBean item : items) {
			String sql = "update shop_info set score="
					+ ((ShopItemBean) item.getBean()).getScore() + " where id="
					+ item.getId();

			sqls.add(sql);
		}

		return ExecuteMultiSQL(sqls);

		// for( ProcessItemBean item : items ){
		// String sql =
		// "update shop_info set score="+item.getScore()+" where id="+item.getId();
		// ExecuteSQL( sql );
		// }
		// return true;
	}
}
